package gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import commands.LoginCheckCommand;

import containers.StudentContainer;
import entities.CoSigner;
import entities.Student;

/**
 * 
 * @author eks691
 * 
 */
public class EmployeeCosignerInfoPanel extends JPanel {

	/** Student who's loan is being looked at */
	Student student;

	/** Cosigner for the student */
	CoSigner cosigner;

	/** id for the employee using the system */
	int employeeId;

	/** id for the student account the employee is reviewing */
	int studentId;

	/**
	 * Constructor
	 * 
	 * @param s
	 *            id for the student account the employee is reviewing
	 * @param e
	 *            id for the employee using the system
	 */
	public EmployeeCosignerInfoPanel(int s, int e) {
		studentId = s;
		employeeId = e;
		student = null;
		cosigner = null;

		if (StudentContainer.dictionary().containsKey(s)) {
			student = StudentContainer.dictionary().get(s);
			cosigner = student.getLoan().getCosigner();
		}

		if (cosigner != null) {
			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			add(new StudentInfoPanel());
			add(new LoanInfoPanel());
			add(new CoSignerInfoPanel());
			add(new FinInfoPanel());
			add(new ButtonPanel());
		}
	}

	private class StudentInfoPanel extends JPanel {

		private static final long serialVersionUID = 1L;

		/**
		 * Construct a panel with the current information for the current
		 * student
		 */
		private StudentInfoPanel() {
			setBorder(new TitledBorder(new EtchedBorder(),
					"Student Personal Information"));
			setLayout(new GridLayout(5, 2));

			JLabel name = new JLabel("Name: " + student.getName());
			add(name);
			JLabel id = new JLabel("ID: " + student.getID());
			add(id);
		}
	}

	/**
	 * Construct a panel for the loan information of the student
	 */
	private class LoanInfoPanel extends JPanel {

		private static final long serialVersionUID = 1L;

		private LoanInfoPanel() {
			setBorder(new TitledBorder(new EtchedBorder(), "Loan Information"));
			setLayout(new GridLayout(6, 1));

			JLabel loanStatus;
			if (student.getLoan().isNew()) {
				loanStatus = new JLabel("Loan Status: New");
			} else {
				loanStatus = new JLabel("Loan Status: Not New");
			}
			add(loanStatus);

			JLabel loanStatus2;
			if (student.getLoan().isApproved()) {
				loanStatus2 = new JLabel("Approved: Yes");
			} else {
				loanStatus2 = new JLabel("Approved: No");
			}
			add(loanStatus2);

			JLabel amtRequested = new JLabel("Amount Requested: $"
					+ student.getLoan().getAmountRequested());
			add(amtRequested);
			JLabel amtReceived = new JLabel("Amount Approved: $"
					+ student.getLoan().getAmountGiven());
			add(amtReceived);
		}
	}

	private class CoSignerInfoPanel extends JPanel {
		private static final long serialVersionUID = 1L;

		/**
		 * Construct a panel with the current information for the current
		 * cosigner
		 */
		private CoSignerInfoPanel() {
			setBorder(new TitledBorder(new EtchedBorder(),
					"CoSigner Personal Information"));
			setLayout(new GridLayout(5, 2));

			JLabel name = new JLabel("Name: " + cosigner.getName());
			add(name);
			JLabel birthday = new JLabel("Birthday: "
					+ cosigner.getBirthdateAsString());
			add(birthday);
			JLabel address = new JLabel("Address: " + cosigner.getAddress());
			add(address);
			JLabel phoneNumber = new JLabel("Phone Number: "
					+ cosigner.getPhoneNumber());
			add(phoneNumber);
			JLabel email = new JLabel("Email: " + cosigner.getEmail());
			add(email);
		}
	}

	/**
	 * Construct a panel for the financial information of the cosigner
	 */
	private class FinInfoPanel extends JPanel {

		private static final long serialVersionUID = 1L;

		private FinInfoPanel() {
			setBorder(new TitledBorder(new EtchedBorder(),
					"Cosigner Financial Information"));
			setLayout(new GridLayout(8, 1));

			JLabel householdIncome = new JLabel("Household Income: $"
					+ cosigner.getFinancialInfo().getHouseholdIncome());
			add(householdIncome);
			JLabel personalIncome = new JLabel("Personal Income: $"
					+ cosigner.getFinancialInfo().getPersonalIncome());
			add(personalIncome);
			JLabel scholarship = new JLabel("Scholarship Amount: $"
					+ cosigner.getFinancialInfo().getScholarship());
			add(scholarship);
			JLabel taxableEarnings = new JLabel("Taxable Earnings: $"
					+ cosigner.getFinancialInfo().getTaxableEarnings());
			add(taxableEarnings);
			JLabel monthlyExpenses = new JLabel("Monthly Expenses: $"
					+ cosigner.getFinancialInfo().getMonthlyExpenses());
			add(monthlyExpenses);
			JLabel childSupport = new JLabel("Child Support: $"
					+ cosigner.getFinancialInfo().getChildSupport());
			add(childSupport);
		}
	}

	/**
	 * Construct a panel for the buttons for the panel
	 */
	private class ButtonPanel extends JPanel {

		private static final long serialVersionUID = 1L;

		private ButtonPanel() {
			setLayout(new GridLayout(1, 2, 50, 50));

			JButton changeInfo = new JButton("Update Cosigner Information");
			add(changeInfo);
			JButton back = new JButton("Back");
			add(back);

			// add listeners for the buttons
			back.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					getTopLevelAncestor().setVisible(false);
					MainMenuFrame frame = new MainMenuFrame(
							LoginCheckCommand.UserType.EMPLOYEE, employeeId);
					frame.setVisible(true);
					EmployeeAllStudentInfoPanel newPanel = new EmployeeAllStudentInfoPanel(
							student, employeeId);
					frame.changePanel(newPanel);
				}
			});

			changeInfo.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					getTopLevelAncestor().setVisible(false);
					MainMenuFrame myFrame = new MainMenuFrame(
							LoginCheckCommand.UserType.EMPLOYEE, employeeId);
					myFrame.setVisible(true);
					EmployeeCosignerChangeInfoPanel newPanel = new EmployeeCosignerChangeInfoPanel(
							student.getID(), employeeId);
					myFrame.changePanel(newPanel);
				}
			});

		}
	}
}
